﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Smartec.Accounts.Camel.AMS.Model.Entities;

namespace Smartec.Accounts.Camel.AMS.Model.Repository
{
    internal static class RoleControllerRepository
    {
        //
        // Query Methods

        public static IEnumerable<AMSController> FindAllControllers()
        {
            return AMSObjectContextHelper.CurrentObjectContext.AMSControllers.OrderBy(o=>o.DisplayName)
                .AsEnumerable();
        }

        public static IEnumerable<RoleController> GetRoleControllers(string roleName)
        {
            return AMSObjectContextHelper.CurrentObjectContext.RoleControllers.Where(o => o.Role.RoleName == roleName);
        }

        public static RoleController GetRoleController(string controllerName, string roleName)
        {
            return AMSObjectContextHelper.CurrentObjectContext.RoleControllers.Where(o => o.Controller.Name.ToLower() == controllerName.ToLower() && o.Role.RoleName.ToLower() == roleName.ToLower()).FirstOrDefault();
        }

        public static Role GetRole(string roleName)
        {
            return AMSObjectContextHelper.CurrentObjectContext.Roles.SingleOrDefault(o => o.RoleName == roleName);
        }

        public static AMSController GetController(int id)
        {
            return AMSObjectContextHelper.CurrentObjectContext.AMSControllers.SingleOrDefault(d => d.ID == id);
        }


        public static void Delete(RoleController roleController)
        {
            AMSObjectContextHelper.CurrentObjectContext.RoleControllers.DeleteObject(roleController);
        }

        //
        // Persistence 

        public static void Save()
        {
            AMSObjectContextHelper.CurrentObjectContext.SaveChanges();
        }

        // Helper Methods
    }
}
